<?php

/* ======================================================================= */
/* == Titan Slider ======================================================= */
/* ======================================================================= */

/* 

Author - WPTitans
Code Name - Levathian
Version - 0.2
Description - layout builder works on Hades Framework only.

*/

if(!defined('HPATH'))
die(' The File cannot be accessed directly ');

if(!class_exists('LayoutBuilder')) {

class LayoutBuilder extends Loki {
	
	function __construct () { parent::__construct('Layout Manager');  }
	function manager_admin_init(){	
	global $wpdb;
	 
	  $data = array();
	
	 if(isset($_POST['action']) &&  $_GET['page']=="LAYO" ) :
	
	foreach ( $_POST['widget_type'] as $key => $value ) :
	
	$data[] = array( "widget_name" => $value , "category_id" => $_POST['category_id'][$key]);
	
	endforeach;
	
	update_option(SN."_layouts",serialize($data));
	header("Location: admin.php?page=LAYO&saved=true");
    die; 
	endif;
	
	 
	 wp_enqueue_script('thickbox');
	 wp_enqueue_style('thickbox');
	 wp_enqueue_script('jquery-ui-sortable');
	 wp_enqueue_script('jquery-ui-draggable');
	 wp_enqueue_script('jquery-ui-droppable');
	 }	
	function manager_admin_wrap(){	
	global $wpdb;
	
	$category_ids = get_all_category_ids();
	$select_box = '<select class="select-box">';
	foreach($category_ids as $cat_id) {
  		$cat_name = get_cat_name($cat_id);
  		$select_box = $select_box . "<option value='".$cat_id."'>". $cat_name."</option>";
	}
$select_box = $select_box . '</select>';
	?>
	
    <script type="text/javascript">
	
	jQuery(function($){
		var obj,temp,s;
		var block = jQuery('<div class=""><h4></h4> <a href="" class="close"></a> <input type="hidden" name="widget_type[]" class="widget_type" /> <input type="hidden" name="category_id[]" class="category_id" value="<?php echo $category_ids[0]; ?>" /> </div>');
		var sel = $('.select-box').first();
		var canvas = $( ".canvas" );
		
		canvas.sortable();
		$('.holder .draggable').draggable({ helper: 'clone' });
		$( ".canvas" ).droppable({
			 greedy:true, accept: '.draggable' ,
			drop: function( event, ui ) {
			 obj = ui.draggable;
			
			if(obj.hasClass('three-columns'))
			{
				temp = block.clone().addClass('three-columns element').append('<div class="one_third"></div><div class="one_third"></div><div class="one_third"></div>');
				$(this).append(temp);
				temp.find('h4').html('Three Column Widget');
				temp.find('.one_third').append(sel.clone());
				
				temp.find('.widget_type').val('three-columns');
				
			}
			else if(obj.hasClass('posts-image'))
			{
				s = sel.clone();
				
				temp = block.clone().addClass('posts-image element').append(s);
				$(this).append(temp);
				temp.find('h4').html('Grid widget');
				s.wrap('<p class="select-wrapper"/>')
				temp.find('.widget_type').val('posts-image');
			}
			else if(obj.hasClass('full-accordion'))
			{
				s = sel.clone();
				temp = block.clone().addClass('full-accordion element').append(s);
				$(this).append(temp);
				temp.find('h4').html('Full Accordion Widget');
				s.wrap('<p class="select-wrapper"/>')
				temp.find('.widget_type').val('full-accordion');
			}
			 canvas.sortable();
			}
		});

  		$('.select-box').live('change',function(){ 
		obj = $(this).parents('.element');
		
		if(obj.hasClass('three-columns'))
		{
			
			temp ='';
			obj.children('.one_third').each(function(){ temp = temp + $(this).find('select').val() + ":"; });
			obj.find('.category_id').val(temp); 
		}
		else
		obj.find('.category_id').val($(this).val()); 
		
		});
		
		$('.layout-builder .canvas .element .close').live("click",function(e){ obj = $(this).parent(); obj.fadeOut('fast',function(){ obj.remove(); }); e.preventDefault(); });
        
		});
	
	</script>
	
    <?php if( isset($_GET['saved'])) echo "<div class='success_message show'><p>Layout Saved </p> </div>"; ?>
    <?php if( isset($_GET['deleted'])) echo "<div class='success_message show'><p>Layout Deleted </p> </div>"; ?>
    
    <div class="hades_wrap">
     
    <?php echo '<div class="hide">'.$select_box."</div>"; ?>
     
     <form method="post" enctype="multipart/form-data" >
     <div class="panel">
     <input type="submit" class="button-primary" value="Save" />
     </div>
       <div class="layout-builder clearfix">
        <div class="canvas">
        <?php 
		$layouts = unserialize(get_option(SN."_layouts"));
	
		if(!is_array($layouts) ) 	$layouts = array();
		foreach($layouts as $layout)
		{
			switch($layout['widget_name'])
			{
				case 'three-columns' : 
					$ids  = explode(":",$layout['category_id']);
					
					
					
					echo '<div class="element three-columns"><h4>Three Column Widget</h4> <a href="" class="close"></a> <input type="hidden" name="widget_type[]" class="widget_type" value="'.$layout['widget_name'].'" /> <input type="hidden" name="category_id[]" class="category_id" value="'.$layout['category_id'].'" /> ';
				
				for($i=0;$i<3;$i++)
				{
					$select_box = '<select class="select-box">';
					foreach($category_ids as $cat_id) {
						$cat_name = get_cat_name($cat_id);
						if($ids[$i]==$cat_id)
						$select_box = $select_box . "<option value='".$cat_id."' selected=\"selected\">". $cat_name."</option>";
						else
						$select_box = $select_box . "<option value='".$cat_id."'>". $cat_name."</option>";
						
						}
					$select_box = $select_box . '</select>';
					echo ' <div class="one_third">'.$select_box.'</div>';
				}
				
				echo "</div>";
				break;
				
				case 'posts-image' : 
					
					$select_box = '<select class="select-box">';
					foreach($category_ids as $cat_id) {
						$cat_name = get_cat_name($cat_id);
						if($layout['category_id']==$cat_id)
						$select_box = $select_box . "<option value='".$cat_id."' selected=\"selected\">". $cat_name."</option>";
						else
						$select_box = $select_box . "<option value='".$cat_id."'>". $cat_name."</option>";
						
						}
					$select_box = $select_box . '</select>';

					echo '<div class="element posts-image"><h4>Grid widget</h4> <a href="" class="close"></a> <input type="hidden" name="widget_type[]" class="widget_type" value="'.$layout['widget_name'].'" /> <input type="hidden" name="category_id[]" class="category_id" value="'.$layout['category_id'].'" /> <p class="select-wrapper">'.$select_box.'</p></div>';
				
				break;
				
				case 'full-accordion' : 
					
					$select_box = '<select class="select-box">';
					foreach($category_ids as $cat_id) {
						$cat_name = get_cat_name($cat_id);
						if($layout['category_id']==$cat_id)
						$select_box = $select_box . "<option value='".$cat_id."' selected=\"selected\">". $cat_name."</option>";
						else
						$select_box = $select_box . "<option value='".$cat_id."'>". $cat_name."</option>";
						
						}
					$select_box = $select_box . '</select>';
					
					echo '<div class="element full-accordion"><h4>Full Accordion Widget</h4> <a href="" class="close"></a> <input type="hidden" name="widget_type[]" class="widget_type" value="'.$layout['widget_name'].'" /> <input type="hidden" name="category_id[]" class="category_id" value="'.$layout['category_id'].'" /> <p class="select-wrapper">'.$select_box.'</p></div>';
				
				break;
			}
		}
		?>
        
        </div>
        
         <div class="widget-bar">
       		<h2 class="title"> Layout Widgets </h2>
       
       		<div class="holder">
       
       			<div class="draggable three-columns"> 3 Columns </div>
       			<div class="draggable posts-image">Grid Widget </div>
      			 <div class="draggable full-accordion"> Full Width Accordion </div>
            </div>
        </div> 
       </div>
       
       <input type="hidden" name="action" value="save" /> 
       </form>
    </div>  
	  
      
	  
	  <?php
	

	
	 }	 
	 
	 
	
	
	
	}

}

$c = new LayoutBuilder();

